---
title: "Run pipe with memory"
description: "Learn how to run a pipe with memory using BaseAI."
tags:
    - baseai
    - langbase
    - learn
    - pipe
    - memory
section: "nodejs"
published: 2024-09-24
modified: 2024-09-24
---

# Run pipe with memory

### Learn how to run a pipe with memory using BaseAI

<Note sub="/learn">
This guide is part of the /learn BaseAI course. For context, [start from the beginning](/learn) to follow along.
</Note>

---

In this learn guide, you will run a pipe with memory using BaseAI.

---

## Step 1: Update user message

Let's update the user message to the following.

```txt
How to create a pipe?
```

<CodeGroup exampleTitle="Chat with docs RAG" title="Integrate memory with the pipe">

```ts {{ title: 'index.ts' }}
import 'dotenv/config';
import {Pipe} from '@baseai/core';
import pipeSummarizer from './baseai/pipes/summarizer';

const pipe = new Pipe(pipeSummarizer());

const userMsg = `How to create a pipe?`;

async function main() {
	const response = await pipe.run({
		messages: [{role: 'user', content: userMsg}],
		stream: false,
	});

	console.log(response.completion);
}

main();
```

```ts {{ title: './baseai/pipes/summarizer.ts' }}
import { PipeI } from '@baseai/core';
import getCurrentWeatherTool from '../tools/get-current-weather';

const pipeSummarizer = (): PipeI => ({
	apiKey: process.env.LANGBASE_API_KEY!, // Replace with your API key https://langbase.com/docs/api-reference/api-keys
	name: 'summarizer',
	description: 'A pipe that summarizes content and make it less wordy',
	status: 'public',
	model: 'openai:gpt-4o-mini',
	stream: true,
	json: false,
	store: true,
	moderate: true,
	top_p: 1,
	max_tokens: 1000,
	temperature: 0.7,
	presence_penalty: 1,
	frequency_penalty: 1,
	stop: [],
	tool_choice: 'auto',
	parallel_tool_calls: false,
	messages: [
		{ role: 'system', content: `You are a helpful AI assistant.` },
		{ role: 'system', name: 'rag', content: "Below is some CONTEXT for you to answer the questions. ONLY answer from the CONTEXT. CONTEXT consists of multiple information chunks. Each chunk has a source mentioned at the end.\n\nFor each piece of response you provide, cite the source in brackets like so: [1].\n\nAt the end of the answer, always list each source with its corresponding number and provide the document name. like so [1] Filename.doc.\n\nIf you don't know the answer, just say that you don't know. Ask for more context and better questions if needed." }
	],
	variables: [],
    memory: [chatWithDocsMemory()],
    tools: [getCurrentWeatherTool()]
});

export default pipeSummarizer;
```

```ts {{ title: './baseai/tools/get-current-weather.ts' }}
import {ToolI} from '@baseai/core';

export async function getCurrentWeather(location: string, unit: string) {
	return `Weather in ${location} is 72 degrees ${unit === 'celsius' ? 'Celsius' : 'Fahrenheit'}`;
}

const getCurrentWeatherTool = (): ToolI => ({
	run: getCurrentWeather,
	type: 'function' as const,
	function: {
		name: 'getCurrentWeather',
		description: 'Get the current weather for a given location',
		parameters: {
			type: 'object',
			properties: {
				location: {
					type: 'string',
					description: 'The city and state, e.g. San Francisco, CA',
				},
				unit: {
					type: 'string',
					enum: ['celsius', 'fahrenheit'],
				},
			},
			required: ['location'],
		},
	},
});

export default getCurrentWeatherTool;
```
</CodeGroup>

## Step #2: Start baseai server

To run the pipe locally, you need to start the BaseAI server. Run the following command in your terminal:

```bash
npx baseai@latest dev
```

## Step #3: Run Pipe with tsx

Run the `index.ts` file using the following command:

```bash
npx tsx index.ts
```

It will prompt the LLM model to get answers to your questions. It will search the memory and provide you with the best possible answer to your question.

For instance, if you ask "How to create a pipe?", it gives you the answer from the memory.

```txt
To create a Pipe, you need to follow these steps:

1. Select any LLM model (by default, OpenAI gpt-4o-mini is selected).
2. If the Pipe is of type generate, simply run it.
3. If it is a chat pipe, write "hello" in Playground and then run the Pipe.

Additionally, keep in mind that when you create a Pipe, it already contains a readme which can be edited at any time [1].

Sources:
[1] pipe faqs.txt
```

This all happens locally on your machine and you should see the response in your terminal.

---

_In the next learn guide, we will deploy the AI pipe and memory on Langbase to use it as a highly scalable API._

---
